<template>
  <div class="order-container">
    <div class="header">
      <a
        href="javascript:void(0)"
        @click="$router.push({ name: 'PersonalCenter' })"
        class="back-button"
      >
        <img src="./me-img/back.png" alt="返回" width="40%" height="100%" />
        <p>返回</p>
      </a>
      <img src="./me-img/logo.png" alt="logo" class="logo" />
    </div>

    <div class="body">
      <div class="tabs">
        <div
          v-for="tab in tabs"
          :key="tab.id"
          :class="['tab', { active: activeTab === tab.id }]"
          @click="activeTab = tab.id"
        >
          {{ tab.name }}
        </div>
      </div>

      <div class="order-list">
        <div v-for="order in filteredOrders" :key="order.id">
          <!-- 浏览足迹 -->
          <div class="order-item">
            <img :src="order.image" alt="商品图片" class="product-image" />
            <div class="product-info">
              <h4 class="product-name">
                {{ order.name
                }}<button
                  v-if="order.status === 'collectProducts'"
                  class="console-btn"
                >
                  取消收藏
                </button>
              </h4>
              <p class="product-price">单价：{{ order.price }}￥</p>
              <div class="product-specs-box">
                <p class="product-specs">商品详情：{{ order.specs }}</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import orderData from "./meLike.json";

export default {
  name: "CollectList",
  data() {
    return {
      activeTab: this.$route.query.tab || "collectMerchants",
      tabs: [
        { id: "browseFootprints", name: "浏览足迹" },
        { id: "collectProducts", name: "商品收藏" },
      ],
      //orders: orderData.products,
      orders: JSON.parse(localStorage.getItem("favorites")) || [],
    };
  },
  computed: {
    filteredOrders() {
      return this.orders.filter((order) => order.status === this.activeTab);
    },
  },
  watch: {
    // 监听路由变化，更新 activeTab
    "$route.query.tab"(newTab) {
      if (newTab) {
        this.activeTab = newTab;
      }
    },
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
}

/* 保持原有样式不变 */
.order-container {
  min-height: 100vh;
  background-color: #ffffff;
  min-width: 1000px; /* 添加最小宽度防止布局压缩 */
  overflow-x: auto; /* 允许横向滚动 */
}

.header {
  background-color: #f4efd2;
  height: 50px;
  align-items: center; /* 保持垂直居中 */
  box-shadow: #bdbbb7 0px 2px 8px;
  padding: 2px 0px;
  display: flex;
}

.header,
.body {
  width: 60%;
  margin: 0px auto;
  overflow: hidden;
}

.body {
  margin: 30px auto;
  height: 700px;
  background-color: #ffffff;
}

.logo {
  width: 10%;
  height: auto;
  margin-left: auto; /* 关键：将 logo 推到最右侧 */
}

.back-button {
  color: #333;
  display: flex;
  width: 10%;
  height: 100%;
  text-decoration: none;
  line-height: 40px; /* 文字垂直居中 */
  overflow: hidden; /* 防止内容溢出 */
}

.tabs {
  width: 100%;
  height: 10%;
  background-color: #f4efd2;
  margin: 0px 20px 0px 0px;
  box-shadow: #bdbbb7 0px 0px 5px;
  border-radius: 10px 10px 0px 0px;
  display: flex;
  border-bottom: 1px solid #f0f0f0;
}

.tab {
  flex: 1; /* 元素在弹性容器中会占据剩余的所有可用空间 */
  margin: 20px;
  font-size: 15px;
  display: flex;
  justify-content: center; /* 垂直方向居中 */
  align-items: center; /* 水平方向居中 */
  background-color: #ffffff;
  border: 1px solid rgb(212, 211, 211);
  width: 35%;
  padding: 15px 0;
  color: black;
  border-radius: 5px;
}

.tab.active {
  background-color: #d9c596;
  box-shadow: #bdbbb7 0px 0px 5px;
}

.order-list {
  width: 100%;
  height: 90%;
  box-shadow: #bdbbb7 0px 0px 5px;
  background-color: #f4efd2;
  padding: 20px 30px;
  overflow-y: auto; /* 垂直滚动 */
}

.order-item {
  width: 100%;
  padding: 15px;
  background-color: white;
  border-radius: 8px;
  display: flex;
  height: 150px;
  box-shadow: #bdbbb7 0px 0px 3px;
  margin: 10px 0px;
}

.product-image {
  width: 15%;
  height: 100%;
  border-radius: 5px;
  margin: 0px 15px;
}

.product-image-box {
  display: flex;
  flex-direction: column;
  justify-content: space-around; /* 垂直方向居中 */
  align-items: center; /* 水平方向居中 */
  width: 20%;
  height: 100%;
}

.product-info {
  width: 85%;
  margin: 0px 15px;
  background-color: #f4efd2;
  padding: 10px 20px;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.product-specs-box {
  background-color: white;
  margin-top: 10px;
  height: 60px;
  overflow-y: auto; /* 垂直滚动 */
}

.product-specs {
  overflow-y: auto; /* 垂直滚动 */
}

p {
  font-size: 14px;
}
.console-btn {
  height: 25px;
  width: 80px;
  border-radius: 5px;
  float: right;
  border: none;
  box-shadow: #bdbbb7 0px 0px 3px;
  background-color: rgb(237, 129, 51);
}
</style>
